package com.exonline.backstage.controller.menu;

import com.exonline.commons.base.IBaseService;
import com.exonline.backstage.entity.Menu;
import com.exonline.commons.dao.DaoImpl;
import com.exonline.commons.dao.IDao;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class MenuService implements IBaseService<Menu> {
    IDao<Menu> menuIDao = new DaoImpl<>();

    @Override
    public List<Menu> queryAll(int pageSize, int pageNo) throws SQLException {
        return menuIDao.queryWithPagingAndConditions("SELECT * FROM menu",null,pageNo,10,new BeanListHandler<>(Menu.class));
    }

    @Override
    public Menu queryOne(String id) throws SQLException {
        return  menuIDao.queryForObject("select * from menu where id = ?", new BeanHandler<>(Menu.class), id);
    }

    @Override
    public void add(Object... obj) throws SQLException {
        menuIDao.updateWithTransaction("INSERT INTO menu VALUES(?,?,?,?)",obj);

    }

    @Override
    public void update(Object... obj) throws SQLException {
        menuIDao.updateWithTransaction("UPDATE menu SET mname = ?,url=? WHERE id = ?",obj);
    }

    @Override
    public void deleteById(String id) throws SQLException {
        menuIDao.updateWithTransaction(" DELETE FROM menu WHERE id = ?",id);

    }
}
